﻿package cn.poco.pepsi.adapter;

import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import cn.poco.pepsi.R;
import cn.poco.pepsi.bean.Restaurant;
import cn.poco.pepsi.util.Cons;
/**
 * s搜索结果页适配器
 * @author tanglx
 *
 */
public class SearchResultListAdapter extends BaseAdapter {

	private final static String TAG = "SearchResultListAdapter";
	private List<Restaurant> restaurant;
	private Context context;
	private LayoutInflater myinflater;

	public SearchResultListAdapter(Context context, List<Restaurant> restaurant, Handler handler) {
		this.context = context;
		this.restaurant = restaurant;
		this.myinflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return restaurant.size();
	}

	@Override
	public Object getItem(int position) {
		return restaurant.get(position);
	}

	@Override
	public long getItemId(int position) {
		return Long.parseLong(restaurant.get(position).getId().toString());
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolderItem holder = null;
		if (convertView == null) {
			holder = new ViewHolderItem();
			int item;
			if (context.getClass().toString().contains("NearActivity")) {
				item=R.layout.near_item;
			}else {
				item=R.layout.search_result_item;
			}
			convertView = myinflater.inflate(item, null);
			holder.title = (TextView) convertView.findViewById(R.id.title);
			//设置字体黑体
			TextPaint tp = holder.title.getPaint(); 
			tp.setFakeBoldText(true);
			holder.image = (ImageView) convertView.findViewById(R.id.star);
			holder.average = (TextView) convertView.findViewById(R.id.average);
			holder.dish = (TextView) convertView.findViewById(R.id.dish);
			holder.circle = (TextView) convertView.findViewById(R.id.circle);
			holder.join = (ImageView) convertView.findViewById(R.id.join);
			holder.pepsi = (ImageView) convertView.findViewById(R.id.pepsi);
			holder.discount = (ImageView) convertView.findViewById(R.id.discount);
			holder.distance =(TextView)convertView.findViewById(R.id.distance);
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolderItem) convertView.getTag();
		}

		Restaurant res = restaurant.get(position);
		
		holder.title.setText(res.getTitle());
		
		if (res.getVouch() == 1) {
			holder.join.setVisibility(View.VISIBLE);
		}else {
			holder.join.setVisibility(View.GONE);
		}
		if (res.getPepsi() == 1) {
			holder.pepsi.setVisibility(View.VISIBLE);
		}else {
			holder.pepsi.setVisibility(View.GONE);
		}
		if (res.getDiscount() == 1) {
			holder.discount.setVisibility(View.VISIBLE);
		}else {
			holder.discount.setVisibility(View.GONE);
		}
		
		// 设置餐厅星级
		int start = Integer.parseInt(res.getStar());
		switch (start) {
		case Cons.ONE_STAR:
			holder.image.setImageResource(R.drawable.x1);
			break;
		case Cons.TWO_STAR:
			holder.image.setImageResource(R.drawable.x2);
			break;
		case Cons.THREE_STAR:
			holder.image.setImageResource(R.drawable.x3);
			break;
		case Cons.FOUR_STAR:
			holder.image.setImageResource(R.drawable.x4);
			break;
		case Cons.FIVE_STAR:
			holder.image.setImageResource(R.drawable.x5);
			break;
		default:
			holder.image.setImageResource(R.drawable.x0);
			break;
		}
		holder.average.setText("人均：" + res.getAverage());
		
		if(res.getDish().equals("")){
			holder.dish.setText("特色美食");
		}else{
			holder.dish.setText(res.getDish());
		}
		if(res.getArea().equals("")){
			holder.circle.setText("区域不详");
		}else{
			holder.circle.setText(res.getArea());
		}
		
		if (context.getClass().toString().contains("NearActivity")) {
			holder.distance.setVisibility(View.VISIBLE);
			holder.distance.setText(res.getDistance()+"米");
		}
		
		return convertView;
	}

	public final class ViewHolderItem {
		public TextView title;
		public ImageView join;
		public ImageView pepsi;
		public ImageView discount;
		public ImageView image;
		public TextView average;
		public TextView dish;
		public TextView circle;
		public TextView  distance;
	}


}
